package kr.co.hk;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class FacDetails extends Activity {
	private String phoneNum;
	private String homepage;
	private String email;
	private String addr;

	EditText etDetail;


	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.fac_details);
		

		etDetail = (EditText) findViewById(R.id.etDetail);


		Bundle extra = getIntent().getExtras();

		if (extra != null) {
			String str = extra.getString("seq");

			String html;
			StringBuilder purposeHtml = new StringBuilder();
			html = getFacDetail("http://www.women.go.kr/new_women/women/info/fac/selectFacDetl.do?seq="
					+ str);
			// �ü��� ã��
			String organSou = html.substring(html.indexOf("�ü���</th>"), html
					.indexOf("�з��</th>"));
			String organDes = organSou.substring(organSou.indexOf("<td>") + 4,
					organSou.indexOf("</td>"));
			purposeHtml.append(organDes + "\n");
			// �з�� ã��
			String groupSou = html.substring(html.indexOf("�з��</th>"), html
					.indexOf("�ּ�</th>"));
			String groupDes = groupSou.substring(groupSou.indexOf("<td>") + 4,
					groupSou.indexOf("</td>"));
			purposeHtml.append(groupDes + "\n");
			// �ּ� ã��
			String addrSou = html.substring(html.indexOf("�ּ�</th>"), html
					.indexOf("���ü</th>"));
			String addrDes = addrSou.substring(
					addrSou.indexOf("<td colspan") + 16, addrSou
							.indexOf("&nbsp;<a href="));
			purposeHtml.append(addrDes + "\n");

			// � ��ü ã��
			String runSou = html.substring(html.indexOf("���ü</th>"), html
					.indexOf("��ǥ�ڸ�</th>"));
			String runDes = runSou.substring(runSou.indexOf("<td>") + 4, runSou
					.indexOf("</td>"));
			purposeHtml.append(runDes + "\n");
			// ��ǥ�ڸ� ã��
			String repSou = html.substring(html.indexOf("��ǥ�ڸ�</th>"), html
					.indexOf("��ǥ Tel</th>"));
			String repDes = repSou.substring(runSou.indexOf("<td>") + 4, repSou
					.indexOf("</td>"));
			purposeHtml.append(repDes + "\n");
			// ��ǥtel ã��
			String telSou = html.substring(html.indexOf("��ǥ Tel</th>"), html
					.indexOf("FAX</th>"));
			String telDes = telSou.substring(telSou.indexOf("<td>") + 4, telSou
					.indexOf("</td>"));
			purposeHtml.append(telDes + "\n");

			// fax ã��
			String faxSou = html.substring(html.indexOf("FAX</th>"), html
					.indexOf("��ǥ�̸��� </th>"));
			String faxDes = faxSou.substring(faxSou.indexOf("<td>") + 4, faxSou
					.indexOf("</td>"));
			purposeHtml.append(faxDes + "\n");
			// ��ǥ �̸��� ã��
			String emailSou = html.substring(html.indexOf("��ǥ�̸��� </th>"), html
					.indexOf("���Ұ�</th>"));
			String emailDes = emailSou.substring(emailSou
					.indexOf("<td colspan") + 16, emailSou.indexOf("</td>"));
			purposeHtml.append(emailDes + "\n");

			// ��� �Ұ� ã��
			String intSou = html.substring(html.indexOf("���Ұ�</th>"), html
					.indexOf("ã�ƿ��±�</th>"));
			String intDes = intSou
					.substring(intSou.indexOf("<td colspan") + 16, intSou
							.indexOf("</td>"));
			purposeHtml.append(intDes + "\n");
			// ã�ƿ��ô� �� ã��
			String waySou = html.substring(html.indexOf("ã�ƿ��±�</th>"), html
					.indexOf("Ȩ�������ּ�</th>"));
			String wayDes = waySou
					.substring(waySou.indexOf("<td colspan") + 16, waySou
							.indexOf("</td>"));
			purposeHtml.append(wayDes + "\n");
			// Ȩ������ ã��
			String pageSou = html.substring(html.indexOf("Ȩ�������ּ�</th>"), html
					.indexOf("����� Tel</th>"));
			String pageDes = pageSou.substring(
					pageSou.indexOf("<td colspan") + 16, pageSou
							.indexOf("</td>"));
			purposeHtml.append(pageDes + "\n");

			// ����� tel ã��
			String tel1Sou = html.substring(html.indexOf("����� Tel</th>"), html
					.indexOf("����� �̸���</th>"));
			String tel1Des = tel1Sou.substring(
					tel1Sou.indexOf("<td colspan") + 16, tel1Sou
							.indexOf("</td>"));
			purposeHtml.append(tel1Des + "\n");
			// ����� �̸��� ã��
			String email2Sou = html.substring(html.indexOf("����� �̸���</th>"),
					html.indexOf("��ǥ �̹��� ����</label>"));
			String email2Des = email2Sou.substring(email2Sou
					.indexOf("<td colspan") + 16, email2Sou.indexOf("</td>"));
			purposeHtml.append(email2Des + "\n");
			// ��ǥ �̹��� ���� ã��
			// ������ü��
			String engSou = html.substring(html.indexOf("������ü��</th>"), html
					.indexOf("������</th>"));
			String engDes = engSou.substring(engSou.indexOf("<td>") + 4, engSou
					.indexOf("</td>"));
			purposeHtml.append(engDes + "\n");
			// ������
			String buildSou = html.substring(html.indexOf("������</th>"), html
					.indexOf("��������</th>"));
			String buildDes = buildSou.substring(buildSou.indexOf("<td>") + 4,
					buildSou.indexOf("</td>"));
			purposeHtml.append(buildDes + "\n");
			// ��������
			String purSou = html.substring(html.indexOf("��������</th>"), html
					.indexOf("�ֿ���</th>"));
			String purDes = purSou
					.substring(purSou.indexOf("<td colspan") + 16, purSou
							.indexOf("</td>"));
			purposeHtml.append(purDes + "\n");
			// �ֿ���
			String busSou = html.substring(html.indexOf("�ֿ���</th>"), html
					.indexOf("��ϰ�û</th>"));
			String busDes = busSou
					.substring(busSou.indexOf("<td colspan") + 16, busSou
							.indexOf("</td>"));
			purposeHtml.append(busDes + "\n");
			// ��ϰ�û
			String regSou = html.substring(html.indexOf("��ϰ�û</th>"), html
					.indexOf("��������</th>"));
			String regDes = regSou.substring(regSou.indexOf("<td>") + 4, regSou
					.indexOf("</td>"));
			purposeHtml.append(regDes + "\n");
			// ��������
			// ���� �� ��ȸ��Ȳ
			String nowSou = html.substring(html.indexOf("���� �� ��ȸ��Ȳ</th>"), html
					.indexOf("�����</th>"));
			String nowDes = nowSou.substring(nowSou.indexOf("<td>") + 4, nowSou
					.indexOf("</td>"));
			purposeHtml.append(nowDes + "\n");
			// �����
			String emSou = html.substring(html.indexOf("�����</th>"), html
					.indexOf("��Ÿ</th>"));
			String emDes = emSou.substring(emSou.indexOf("<td>") + 4, emSou
					.indexOf("</td>"));
			purposeHtml.append(emDes + "\n");
			// ��Ÿ

			addr = organDes; // �ü���
			homepage = pageDes;
			email = emailDes;
			phoneNum = telDes;

			purposeHtml.toString();
			etDetail.setText(purposeHtml);

		}
	}

	String getFacDetail(String addr) {
		StringBuilder html = new StringBuilder();
		try {
			URL url = new URL(addr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			if (conn != null) {
				conn.setConnectTimeout(10000);
				conn.setUseCaches(false);
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					BufferedReader br = new BufferedReader(
							new InputStreamReader(conn.getInputStream(),
									"euc-kr"));
					for (;;) {
						String line = br.readLine();
						if (line == null)
							break;
						html.append(line + '\n');
					}
					br.close();
				}
				conn.disconnect();
			}
		} catch (Exception ex) {
			Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
		}
		return html.toString();
	}
}
